package com.androidtv.pos.ui.fragment;

import android.os.Bundle;
import android.view.View;

import com.androidhelper.module.common.base.BaseFragment;
import com.androidtv.pos.R;
import com.androidtv.pos.adapter.LeftOrderAdapter;
import com.androidtv.pos.controller.CurrentMealController;
import com.androidtv.pos.modelbean.HomeArticleData;

import java.util.ArrayList;
import java.util.Iterator;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class LeftMainFragment extends BaseFragment implements CurrentMealController.onMealClickListener {


    public static LeftMainFragment newInstance() {
        Bundle args = new Bundle();
        LeftMainFragment fragment = new LeftMainFragment();
        fragment.setArguments(args);
        return fragment;
    }

    private RecyclerView mRecyclerView;
    private LeftOrderAdapter mLeftOrderAdapter;

    private ArrayList<HomeArticleData> orderedMealList = new ArrayList();

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        CurrentMealController.getInstance().registerListener(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        CurrentMealController.getInstance().unregisterListener(this);
    }

    @Override
    protected int attachLayoutRes() {
        return R.layout.fragment_main_left;
    }

    @Override
    protected void initView(View view, @Nullable Bundle savedInstanceState) {
        mRecyclerView = view.findViewById(R.id.item_recyclerview);
        mRecyclerView.setHasFixedSize(true);

        LinearLayoutManager gridLayoutManager = new LinearLayoutManager(_mActivity);

        mRecyclerView.setLayoutManager(gridLayoutManager);

        mLeftOrderAdapter = new LeftOrderAdapter(R.layout.article_left_item_cardview_layout);
        mRecyclerView.setAdapter(mLeftOrderAdapter);
        mLeftOrderAdapter.addData(orderedMealList);

        mLeftOrderAdapter.addListener(new LeftOrderAdapter.onButtonClickListener() {
            @Override
            public void onAdapterAddClick(HomeArticleData data) {
                CurrentMealController.getInstance().notifyAccountChange(data, null);
            }

            @Override
            public void onAdapterMinusClick(HomeArticleData data) {
                CurrentMealController.getInstance().notifyAccountChange(null, data);

            }

            @Override
            public void onAdaptrPreviewClick(HomeArticleData data) {

            }
        });
    }

    @Override
    protected void setListener() {

    }

    @Override
    protected void initEventAndData(Bundle savedInstanceState) {

    }

    @Override
    public void onAddClick(HomeArticleData data) {
        notifyList(data);
    }

    @Override
    public void onMinusClick(HomeArticleData data) {
        notifyList(data);
    }

    private void notifyList(HomeArticleData data) {
        if (isDataInList(data)) {
            Iterator<HomeArticleData> iterator = orderedMealList.iterator();//实例化迭代器
            while (iterator.hasNext()) {
                HomeArticleData itData = iterator.next();

                if (itData.getId() == data.getId()) {
                    if (data.getCurrentNum() == 0) {
                        orderedMealList.remove(itData);
                    } else {
                        itData.setCurrentNum(data.getCurrentNum());
                    }
                }
            }
        } else {
            orderedMealList.add(data);
        }

        if (mLeftOrderAdapter.getData().size()==0){
            mLeftOrderAdapter.setNewData(orderedMealList);
        }
        mLeftOrderAdapter.notifyDataSetChanged();
    }

    boolean isDataInList(HomeArticleData data) {

        if (orderedMealList.size()==0){
            return false;
        }
        for (HomeArticleData homeArticleData : orderedMealList) {
            if (homeArticleData.getId() == data.getId()) {
                return true;
            }
        }
        return false;
    }
}
